package com.chm.fw;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chm.framwork.recyclerview.PullLoadMoreRecyclerView;
import com.chm.framwork.recyclerview.RecycleViewDivider;
import com.chm.framwork.recyclerview.RecyclerViewOnScroll;
import com.chm.framwork.utilcode.util.PermissionUtils;
import com.chm.fw.adapter.BaseRecyclerViewAdapter;
import com.chm.fw.base.BaseActivity;
import com.chm.fw.bean.AdapterTypeBean;
import com.chm.fw.ui.BannerActivity;
import com.chm.fw.ui.CameraActivity;
import com.chm.fw.ui.EditTextActivity;
import com.chm.fw.ui.PhotoViewActivity;
import com.chm.fw.ui.QRCodeActivity;
import com.chm.fw.ui.RxActivity;
import com.chm.fw.ui.ToastActivity;
import com.chm.fw.ui.VideoPlayerActivity;
import com.chm.fw.ui.WebViewActivity;
import com.chm.fw.utils.ValueUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity implements PullLoadMoreRecyclerView.PullLoadMoreListener {

    private String[] names = {
            "EditText",
            "Toast",
            "PhotoView",
            "Banner",
            "VideoPlayer",
            "Camera",
            "QRCode",
            "Rx",
            "WebView"
    };

    private void startActivity(int position) {
        Intent intent = null;
        switch (position) {
            case 0://EditText
                intent = new Intent(this, EditTextActivity.class);
                break;
            case 1://Toast
                intent = new Intent(this, ToastActivity.class);
                break;
            case 2://PhotoView 图片放大缩小
                intent = new Intent(this, PhotoViewActivity.class);
                break;
            case 3://Banner 轮播图
                intent = new Intent(this, BannerActivity.class);
                break;
            case 4://VideoPlayer
                intent = new Intent(this, VideoPlayerActivity.class);
                break;
            case 5://Camera 图片上传（头像、图片）
                intent = new Intent(this, CameraActivity.class);
                break;
            case 6://QRCode 二维码
                intent = new Intent(this, QRCodeActivity.class);
                break;
            case 7://rx
                intent = new Intent(this, RxActivity.class);
                break;
            case 8://WebView
                intent = new Intent(this, WebViewActivity.class);
        }
        if (intent == null) {
            return;
        }
        startActivity(intent);
    }

    //运行时权限
    private static String[] PERMISSIONS_CONTACT = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    private PullLoadMoreRecyclerView recyclerView;
    private BaseRecyclerViewAdapter adapter;
    private List<AdapterTypeBean> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        initPermissions();
    }

    private void initPermissions() {
        PermissionUtils.requestPermissions(this, 100, PERMISSIONS_CONTACT, new PermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {

            }

            @Override
            public void onPermissionDenied(String[] deniedPermissions) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtils.onRequestPermissionsResult(this, 100, PERMISSIONS_CONTACT, grantResults);
    }

    private void initData() {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        for (int i = 0; i < names.length; i++) {
            AdapterTypeBean bean = new AdapterTypeBean();
            bean.setType(0);
            Map<String, Object> data = new HashMap<>();
            data.put("position", i);
            data.put("name", names[i]);
            bean.setData(data);
            mData.add(bean);
        }
        AdapterTypeBean bean1 = new AdapterTypeBean();
        bean1.setType(1);
        mData.add(bean1);
    }

    private void initView() {
        recyclerView = findViewById(R.id.a_main_recyclerView);
        recyclerView.setLinearLayout();
        adapter = new BaseRecyclerViewAdapter(this, mData, new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, AdapterTypeBean itemData, int viewType) {
                if (itemData.getType() == 0) {
                    Map<String, Object> data = itemData.getData();
                    startActivity(ValueUtils.getInt(data.get("position")));
                }
            }
        }, R.layout.item_content, R.layout.item_bottom_no_more) {
            @Override
            public void onBindViewHolder(ViewHolder viewHolder, AdapterTypeBean itemData, int position) {
                if (itemData.getType() == 0) {
                    Map<String, Object> data = itemData.getData();
                    viewHolder.getTextView(R.id.i_content_tv).setText(ValueUtils.getString(data.get("name")));
                }
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 0, Color.parseColor("#00000000")));
        recyclerView.setOnPullLoadMoreListener(this);
        recyclerView.addOnScrollListener(new RecyclerViewOnScroll(recyclerView) {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.setPullLoadMoreCompleted();
                //mData.clear();
                //mData.add();
                adapter.notifyDataSetChanged();
                recyclerView.scrollToTop();
            }
        }, 1000);
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.setPullLoadMoreCompleted();
                AdapterTypeBean remove = mData.remove(mData.size() - 1);
                //mData.add();
                mData.add(remove);
                adapter.notifyDataSetChanged();
            }
        }, 1000);
    }
}
